/**
 * 2013-6-24 下午10:29:11 Created By niexiaoqiang
 */

package com.xiaoqiang.MtwV2ForMoblie.update;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import android.os.Looper;

import com.xiaoqiang.MtwV2ForMoblie.AppStartAction;
import com.xiaoqiang.util.Logger;
import com.xiaoqiang.util.download.HttpDownload;

/**
 * 下载配置文件的线程,在AppStartAction中调用
 */
public class MtwDownloadApkFileThread extends Thread {
	private String filehttpurl;
	private String downloadfilename;
	private static Logger log = new Logger(MtwDownloadApkFileThread.class);
	private DownloadDialog downDialog;
	private HttpDownload httpDownload;

	public MtwDownloadApkFileThread(DownloadDialog downDialog) {
		this.downDialog = downDialog;
		this.filehttpurl = getApkHttpUrl();
		this.downloadfilename = filehttpurl.substring(filehttpurl.lastIndexOf(File.separator) + 1);
	}

	private String getApkHttpUrl() {
		Properties mtwinfoProperties = new Properties();
		try {
			mtwinfoProperties.load(new FileInputStream(AppStartAction.MTWROOTDIR + "/mtwInfo.properties"));
		}
		catch (Exception e) {
			log.error(e);
		}
		return mtwinfoProperties.getProperty("MtwAppHttpUrl");
	}

	@Override
	public void run() {
		Looper.prepare();
		httpDownload = new HttpDownload(downloadfilename);
		DownloadDialogApkDownLoadSizeChangedListener listener = new DownloadDialogApkDownLoadSizeChangedListener(downDialog);
		httpDownload.addServerMsgListener(listener);
		try {
			log.debug("start donwlaodfile" + filehttpurl);
			httpDownload.DownFile(filehttpurl);
		}
		catch (Exception e) {
			log.equals(e);
		}
	}

	public void shutdowndonwload() {
		httpDownload.setIsdown(true);
	}
}
